package com.leyou.item.controller;

import com.leyou.page.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService detailService;
    @Autowired
    private SkuService skuService;

    /**
     * 分页查询spu
     *
     * @param page       当前页
     * @param rows       每页大小
     * @param saleable   上架商品或下降商品
     * @param brandId    品牌id
     * @param categoryId 分类id
     * @param id         spu的id
     * @return 当前页商品数据
     */

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                          @RequestParam(value = "saleable", required = false) Boolean saleable,
                                                          @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                          @RequestParam(value = "brandId", required = false) Long brandId,
                                                          @RequestParam(value = "id", required = false) Long id) {

        PageDTO<SpuDTO> pagePpuDTO = spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id);
        return ResponseEntity.ok(pagePpuDTO);
    }

    //修改上下架状态
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,
                                                  @RequestParam("saleable") Boolean saleable) {
        spuService.updateSpuSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据id查询商品的具体信息
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        SpuDTO spuDTO = spuService.queryGoodsById(id);
        return ResponseEntity.ok(spuDTO);
    }

    //新增商品
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {
        spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    //修改商品
    @PutMapping("/spu")
    public ResponseEntity<Void> pudategoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //删除商品 * > _ < *
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id") Long id) {
        return null;
    }

    //根据id查询spuDetail
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> quuerySpuDetail(@RequestParam("id") Long id) {
        SpuDetail byId = detailService.getById(id);
        SpuDetailDTO spuDetailDTO = new SpuDetailDTO(byId);
        return ResponseEntity.ok(spuDetailDTO);
    }

    //根据SpuId查询SKU集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpu(@RequestParam("id") Long id) {
        List<Sku> skus = skuService.query().eq("spu_id", id).list();
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(skus);
        return ResponseEntity.ok(skuDTOS);
    }


    //根据id集合查询SKU集合
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBylist(@RequestParam("ids") List<Long> ids) {
        List<Sku> skus = skuService.listByIds(ids);
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(skus);
        return ResponseEntity.ok(skuDTOS);
    }


    //查询商品规格参数键值对
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(detailService.querySpecValues(id, searching));
    }

    //根据Id查询商品Spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    //批量减库存
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap) {
        this.skuService.minusStock(cartMap);
        return ResponseEntity.ok().build();
    }
    //批量加库存
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> cartMap) {
        this.skuService.plusStock(cartMap);
        return ResponseEntity.ok().build();
    }

}